/**
 * Created by Nose on 2017/3/15.
 */
$(function () {
    FastClick.attach(document.body);

    var bgObj = $("#showIOSActionSheet");
    var popweixObj = $(".weui-actionsheet");
    var bgArr = ['weui-btnbg1', 'weui-btnbg2', 'weui-btnbg1'], thisclss = '';
    /*======weixi====*/
    var $iosActionsheet = $('#iosActionsheet');
    var $iosMask = $('#iosMask');

    function hideActionSheet() {
        $iosActionsheet.removeClass('weui-actionsheet_toggle');
        $iosMask.hide();
    }

    $iosMask.on('click', hideActionSheet);
    /*    $('#iosActionsheetCancel').tap(function(){
            alert(11);
        })*/
    $('#iosActionsheetCancel').on('click', hideActionSheet);

    $("#showIOSActionSheet").on("click", function () {
        $iosActionsheet.addClass('weui-actionsheet_toggle');
        $iosMask.show();
    });

    /*======login====*/
    var SMSbox = $('#SMSbox');

    function smsboxhide() {
        SMSbox.removeClass('weui-actionsheet_toggle');
        $iosMask.hide();
    }

    $iosMask.on('click', smsboxhide);
    $('#SMSboxCancel').on('click', smsboxhide);
    $("#showSMSbox").on("click", function () {
        SMSbox.addClass('weui-actionsheet_toggle');
        $iosMask.show();
    });

    /*用户协议*/
    $('#agtbox').click(function () {
        var bdObj = $(".pop-weixi_bd");
        var introtxtObj = bdObj.find('.introtxt');
        introtxtObj.show();
        bdObj.scrollTo({
            toT: bdObj.find('.imgbox').height() + 30    //滚动目标位置
        });
    });


    var loginboxObj = $(".loginbox", "#SMSbox");
    var introtxt2Obj = $(".introtxt2", "#SMSbox");
    var gobackObj = $(".goback", "#SMSbox");
    $('#agtbox2').click(function () {
        loginboxObj.hide();
        introtxt2Obj.show();
        gobackObj.show();
    });
    gobackObj.click(function () {
        loginboxObj.show();
        introtxt2Obj.hide();
        gobackObj.hide();
    })
    $("#downApp").click(function () {
        var u = navigator.userAgent;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        if (isAndroid) {
            window.location.href = "https://www.pgyer.com/2Mtg";
        } else {
            window.location.href = "https://appsto.re/cn/CD6afb.i";
        }
    });
});

function getCode() {
    var cellphone = $("#loginname").val();
    if (!(/^1(3|4|5|7|8)\d{9}$/.test(cellphone))) {
        alert("手机号码有误，请重填！");
        return false;
    }
    // 加载倒计时
    btnTime();
    $.get("/portal/auth/phone/getPassword?cellphone=" + cellphone, function (result) {
        alert(result.msg);
    });
}

function userAuth() {
    var $loadingToast = $('#loadingToast');
    $('#userAuthForm').ajaxSubmit({
        type: 'post',
        url: '/portal/auth/phone/userAuth',
        beforeSubmit: function () {
            var cellphone = $("#loginname").val();
            var validCode = $("#validCode").val();
            if (!(/^1(3|4|5|7|8)\d{9}$/.test(cellphone))) {
                alert("手机号码有误，请重填！");
                return false;
            }
            var reg = /^\d{6}\b/;
            if (!validCode || !reg.test(validCode)) {
                alert("验证码格式错误，请重试！");
                return false;
            }
            $loadingToast.show();
            return true;
        }, success: function (response) {
            $loadingToast.hide();
            alert(response.msg);
            // window.location.href = "http://portal.yzworld.cn?apMac=" + apMac + "&userMac=" + userMac;
            window.location.href = "http://www.baidu.com";
        }, error: function (resp, responseText, response) {
            $loadingToast.hide();
            alert("认证失败，请联系管理员！");
        }
    });
}

function btnTime() {
    var lucencysmObj = $("#lucencysm");
    var wait = 60;

    function time(btn) {
        if (wait == 0) {
            btn.removeAttr("disabled");
            btn.text("获取验证码");
            wait = 60;
        } else {
            btn.attr("disabled", true);
            btn.text(wait + "秒后重新获取");
            wait--;
            setTimeout(function () {
                time(btn);
            }, 1000)
        }
    }

    time(lucencysmObj);
}

function wxSubmit() {
    var $loadingToast = $('#loadingToast');
    $loadingToast.show();
    $("#userWxAuthForm").submit();
}